import Vue from 'vue'
import Router from 'vue-router'

import Login from '@/views/Login.vue'
import Register from '@/views/Register.vue'
import Index from '@/views/Index.vue'
import Map from '@/views/map/Map.vue'
import UserList from '@/views/user/UserList.vue'
import UserInfo from '@/views/user/UserInfo.vue'
import NotFound from '@/views/404.vue'
import ScenicSpotList from '@/views/scenicSpot/ScenicSpotList.vue'
import ScenicSpotAdd from '@/views/scenicSpot/ScenicSpotAdd.vue'
import ScenicSpotUpdate from '@/views/scenicSpot/ScenicSpotUpdate.vue'
import ScenicSpotComments from '@/views/scenicSpot/ScenicSpotCommentList.vue'
import Statistics from '@/views/statistics/Statistics.vue'
Vue.use(Router)

const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/register',
      name: 'register',
      component: Register
    },
    {
      path: '*',
      name: '/404',
      component: NotFound
    },
    {
      path: '/',
      // redirect: '/map',
      name: 'index',
      component: Index,
      children: [
        { path: 'map', name: '地图', component: Map },
        { path: 'user-list', name: '用户列表', component: UserList },
        { path: 'user-info', name: '用户信息', component: UserInfo },
        { path: 'scenic-spot-list', name: '景区列表', component: ScenicSpotList },
        { path: 'scenic-spot-add', name: '景区新增', component: ScenicSpotAdd },
        { path: 'scenic-spot-update', name: '景区更新', component: ScenicSpotUpdate },
        { path: 'scenic-spot-comments', name: '景区评论', component: ScenicSpotComments },
        { path: 'statistics', name: '统计分析', component: Statistics }
      ]
    }
  ]
})
//  路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    sessionStorage.removeItem('user')
  }
  const user = sessionStorage.getItem('user')
  if (!user && to.path !== '/login' && to.path !== '/register') {
    next('/login')
  } else {
    next()
  }
})

export default router
